﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Portfolio.Entities;
using Portfolio.Repository.Common;
using Portfolio.Repository.Interfaces;

namespace Portfolio.Repository.ArticleCategories
{
    /// <summary>
    /// Klasa Repozytorium Kategorie Artykułów
    /// </summary>
    public class ArticleCategoriesRepository : GenericRepository
    {
        /// <summary>
        /// Konstruktor dziedziczy z klasy Generycznej (ogólnej) Repozytorium, domyślny context=Portfolio3Entities
        /// </summary>
        /// <param name="context"></param>
        public ArticleCategoriesRepository() : base() { }
        
        /// <summary>
        /// Konstruktor dziedziczy z klasy Generycznej (ogólnej) Repozytorium
        /// </summary>
        /// <param name="context"></param>
        public ArticleCategoriesRepository(ObjectContext context) : base(context) { }

        
       

        /// <summary>
        /// Metoda użytkownika
        /// Pobiera z bazy danych wyszukane Kategorie
        /// </summary>
        /// <param name="title">Tytuł</param>
        /// <param name="categoryIDID">Rodzic</param>
        /// <param name="status">Status</param>
        /// <param name="active">Aktywny</param>
        /// <returns>Lista Kategorii</returns>
        public List<ArtcileCategoriesGridV> GetArticleCategoriesBySearch(string pTitle, int? pCategoryIDID, int? pStatus, bool? pActive)
        {
            if (pCategoryIDID == null && pStatus == null && pActive == null)
            {
                return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle)).OrderBy(b => b.SeqAllCategory).ToList();
            }
            else
                if (pCategoryIDID == null && pStatus == null && pActive != null)
                {
                    return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ActiveFlag == pActive).OrderBy(b => b.SeqAllCategory).ToList();
                }
                else
                    if (pCategoryIDID == null && pStatus != null && pActive == null)
                    {
                        return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.Status == pStatus).OrderBy(b => b.SeqAllCategory).ToList();
                    }
                    else
                        if (pCategoryIDID != null && pStatus == null && pActive == null)
                        {
                            return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ArticleCategoryIDID == pCategoryIDID).OrderBy(b => b.SeqAllCategory).ToList();
                        }
                        else
                            if (pCategoryIDID == null && pStatus != null && pActive != null)
                            {
                                return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ActiveFlag == pActive && a.Status == pStatus).OrderBy(b => b.SeqAllCategory).ToList();
                            }
                            else
                                if (pCategoryIDID != null && pStatus == null && pActive != null)
                                {
                                    return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ArticleCategoryIDID == pCategoryIDID && a.ActiveFlag == pActive).OrderBy(b => b.SeqAllCategory).ToList();
                                }
                                else
                                    if (pCategoryIDID != null && pStatus != null && pActive == null)
                                    {
                                        return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ArticleCategoryID == pCategoryIDID && a.Status == pStatus).OrderBy(b => b.SeqAllCategory).ToList();
                                    }
                                    else
                                    {
                                        return Find<ArtcileCategoriesGridV>(a => a.Name.Contains(pTitle) && a.ArticleCategoryID == pCategoryIDID && a.ActiveFlag == pActive && a.Status == pStatus).OrderBy(b => b.SeqAllCategory).ToList();
                                    }
        }

        /// <summary>
        /// Metoda użytkownika
        /// Pobiera z bazy danych konkretną kategorie
        /// </summary>
        /// <param name="articleCategoryID">id konkretnej kategorii</param>
        /// <returns>Kategoria</returns>
        public ArticleCategoryT GetArticleCategoryByIdCategory(int pArticleCategoryID)
        {            
            return Single<ArticleCategoryT>(a => a.ArticleCategoryID == pArticleCategoryID); 
        }

        /// <summary>
        /// Metoda użytkownika
        /// Pobiera z bazy danych kategorie o konkretnej całościowej kolejności
        /// </summary>
        /// <param name="seqAll">Kolejność</param>
        /// <returns>Kategoria</returns>
        public ArticleCategoryT GetArticleCategoryBySeqAll(int pSeqAll)
        {
            return Single<ArticleCategoryT>(a => a.SeqAllCategory == pSeqAll);
        }      

        public int GetMaxSeqArticleCategoriesByIDIDCategory(int? pArticleCategoryIDID)
        {            
            var linq = (from x in Fetch<ArticleCategoryT>()
                        where (pArticleCategoryIDID == null ? x.ArticleCategoryIDID == null : x.ArticleCategoryIDID == pArticleCategoryIDID)
                        select x.SeqCategory);
            if ((linq != null) && (linq.Count() > 0))
            {                
                return linq.Max();
            }
            else
            {
                return 0;
            }
        }

        public int GetMaxSeqAllArticleCategoriesByIDIDCategory(int? pArticleCategoryIDID)
        {
            var linq = (from x in Fetch<ArticleCategoryT>()
                        where (pArticleCategoryIDID == null ? x.ArticleCategoryIDID == null : x.ArticleCategoryIDID == pArticleCategoryIDID)
                        select x.SeqAllCategory);
            if ((linq != null) && (linq.Count() > 0))
            {
                return linq.Max();
            }
            else
            {                
                var linq2 = (from x in Fetch<ArticleCategoryT>()
                            where (x.ArticleCategoryID == pArticleCategoryIDID)
                            select x.SeqAllCategory);
                if ((linq2 != null) && (linq2.Count() > 0))
                {
                    return linq2.Single();
                }
                else
                {
                    return 0;
                }
            }
        }
        
        /// <summary>
        /// Metoda użytkownika
        /// Pobiera z bazy danych listę kategorii z danego rodzica
        /// </summary>
        /// <param name="articleCategoryIDID">Id rodzica lub null</param>
        /// <returns>Lista kategorii</returns>
        public List<ArticleCategoryT> GetArticleCategoriesByIDIDCategoryIsNull(int? pArticleCategoryIDID)
        {
            if (pArticleCategoryIDID == null)
            {
                return Find<ArticleCategoryT>(a => a.ArticleCategoryIDID == null).OrderBy(b => b.SeqCategory).ToList();
            }
            else
            {
                return Find<ArticleCategoryT>(a => a.ArticleCategoryIDID == pArticleCategoryIDID).OrderBy(b => b.SeqCategory).ToList();
            }
        }

        /// <summary>
        /// Metoda użytkwonika
        /// Pobiera wsyztskie kategorie
        /// </summary>
        /// <returns>Lista kategorii</returns>
        public List<ArticleCategoryT> GetAll()
        {
            return GetAll<ArticleCategoryT>().ToList();
        }

        public int GetCountSeqAllArticleCategory()
        {
            var linq = (from x in Fetch<ArticleCategoryT>()
                        select x.SeqAllCategory);
            if ((linq != null) && (linq.Count() > 0))
            {
                return linq.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetCountArticleCategoryChildByArticleCategoryID(int pArticleCategoryID)
        {
            var linq = (from x in Fetch<ArticleCategoryT>()
                        where x.ArticleCategoryIDID == pArticleCategoryID 
                        select x.SeqAllCategory);
            if ((linq != null) && (linq.Count() > 0))
            {
                return linq.Count();
            }
            else
            {
                return 0;
            }
        }
    }
}
